package com.basker.pisces.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.util.CollectionUtils;

import com.basker.pisces.core.query.IQuery.PagingInfo;
import com.basker.pisces.utils.PageUtils;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

public class PageRowList extends ArrayList<RowContract> {

    private static final long serialVersionUID = 2174071692621876814L;

    private int totalCount;
    private int totalPage;
    private int pageIndex;
    private int pageSize;
    private boolean firstPage = false;
    private boolean lastPage = false;

    public PageRowList(Collection<RowContract> collection, int totalCount, int pageIndex, int pageSize) {
        super(collection);

        this.totalCount = totalCount;
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.totalPage = PageUtils.getTotalPageCount(totalCount, pageSize);

        if (CollectionUtils.isEmpty(collection)) {
            this.firstPage = false;
            this.lastPage = false;
        } else {
            this.firstPage = pageIndex == PagingInfo.FIRST_PAGE_INDEX;
            this.lastPage = pageIndex == totalPage;
        }
    }

    public PageRowSet asRowSet() {
        return new PageRowSet(totalCount, totalPage, pageIndex, pageSize, firstPage, firstPage, this);
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public boolean isFirstPage() {
        return firstPage;
    }

    public boolean isLastPage() {
        return lastPage;
    }

    @Getter
    @Setter
    @Builder
    @ToString
    public static class PageRowSet {
        private int totalCount;
        private int totalPage;
        private int pageIndex;
        private int pageSize;
        private boolean firstPage;
        private boolean lastPage;
        private List<RowContract> rows;
    }

}
